import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DesignerPanel extends gamePanel {
    int maxTnt;
    static int designerTnts=0;
    JButton finishButton;
    public DesignerPanel(int rows, int cols, int tntNum) {
        super(rows, cols, 0); // 初始为0个地雷
        this.maxTnt = tntNum;
        //按钮替换 完成设计时将提示换成OK
        topPanel.remove(promptButton);
        finishButton = new JButton("OK");
        finishButton.setFont(new Font("宋体", Font.PLAIN,16));
        finishButton.setEnabled(false);
        finishButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                allowPlay = true;
                timePassed=0;
                finishButton.setEnabled(false);
                //startTimer();
                calculateTntnum();
                //完成后清除炸弹显示
                for (int i=0;i<rows;i++) {
                    for (int j=0;j<cols;j++) {
                        if (cells[i][j].isTnt){
                            cells[i][j].button.setText("");
                        }
                    }
                }
                JOptionPane.showMessageDialog(DesignerPanel.this, "设计已完成，同伴已就位，试炼开始 -"+designerTnts+"雷");
            }
        });
        topPanel.add(finishButton);
        allowPlay = false;
        for (int i=0;i<rows;i++) {
            for (int j=0;j<cols;j++) {
                int r=i;
                int c=j;
                cells[i][j].button.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        if(!allowPlay &&!cells[r][c].isTnt &&designerTnts<maxTnt){
                            cells[r][c].isTnt=true;
                            cells[r][c].button.setText("D");
                            designerTnts++;
                            if(maxTnt==designerTnts){
                                finishButton.setEnabled(true);
                                JOptionPane.showMessageDialog(DesignerPanel.this, "地雷设置完毕，请点击“OK”开始游戏！");
                            }
                        }
                    }
                });
            }
        }
    }

    boolean allowPlay;
    public void openCell(int r, int c) {
        if (!allowPlay) return;
        super.openCell(r, c);
    }
//    public void startTimer() {
//        super.startTimer();
//    }
}
